package com.oauth.global.client.ui.status;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.HasWidgetInfo;
import com.oauth.components.client.ui.HorizontalFlowPanel;
import com.oauth.global.client.beans.ApplicationClientBean;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;

public class StatusPanel extends Composite implements HasText {

	private static StatusUpdateUiBinder uiBinder = GWT.create(StatusUpdateUiBinder.class);

	interface StatusUpdateUiBinder extends UiBinder<Widget, StatusPanel> {
	}

	@UiField
	Button updateButton;

	@UiField
	FlowPanel imagePanel;

	@UiField
	TextArea textArea;

	@UiField
	DeckPanel statusDeckPanel;

	@UiField
	DeckPanel deckPanel;

	private CheckBox[] checkBoxes;

	private Map<Long, HasWidgetInfo> widgetMap;

	public StatusPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		deckPanel.showWidget(0);
		// textArea.addValueChangeHandler(new StatusValueChangeHandler(textArea,
		// updateButton));
	}

	/**
	 * @param widgetMap
	 *            the widgetMap to set
	 */
	public void setWidgetMap(Map<Long, HasWidgetInfo> widgetMap) {
		this.widgetMap = widgetMap;
	}

	public void initImages() {
		imagePanel.clear();
		deckPanel.showWidget(0);
		setLoading(true);
		GlobalRemoteServiceAsync.INSTANCE
				.getApplicationImagesForStatus(new OAuthAsnyCallBack<List<ApplicationClientBean>>() {

					@Override
					public void onSuccess(List<ApplicationClientBean> pApplicationList) {
						if (pApplicationList == null || pApplicationList.isEmpty()) {
							deckPanel.showWidget(1);
						} else {
							setLoading(false);
							int size = pApplicationList.size();
							checkBoxes = new CheckBox[size];
							for (ApplicationClientBean infoBean : pApplicationList) {
								HorizontalFlowPanel element = new HorizontalFlowPanel();
								Image appImage = widgetMap.get(infoBean.getId()).getIcon();
								Image image = new Image(appImage.getUrl());
								image.setTitle(infoBean.getName());
								element.add(image);

								checkBoxes[--size] = new CheckBox(element.getElement()
										.getInnerHTML(), true);
								checkBoxes[size].setValue(true);
								checkBoxes[size].setFormValue("" + infoBean.getId());

								imagePanel.add(checkBoxes[size]);
							}
						}
					}
				});
	}

	@UiHandler("updateButton")
	void onClick(ClickEvent e) {
		updateButton.setEnabled(false);
		setLoading(true);
		Set<Long> applicationIdSet = new HashSet<Long>();

		for (int i = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].getValue()) {
				applicationIdSet.add(Long.parseLong(checkBoxes[i].getFormValue()));
			}
		}

		GlobalRemoteServiceAsync.INSTANCE.updateStatus(textArea.getValue(), applicationIdSet,
				new OAuthAsnyCallBack<Boolean>() {

					@Override
					public void onFailure(Throwable pCaught) {
						showErrorMessage(true);
						updateButton.setEnabled(true);
					}

					@Override
					public void onSuccess(Boolean pArg0) {
						showSuccessMessage(true);
						textArea.setValue("");
						updateButton.setEnabled(true);
					}
				});
	}

	public void setLoading(boolean pLoading) {
		if (pLoading) {
			statusDeckPanel.showWidget(1);
		} else {
			statusDeckPanel.showWidget(0);
		}
	}

	public void showSuccessMessage(boolean pShow) {
		if (pShow) {
			statusDeckPanel.showWidget(2);
		} else {
			statusDeckPanel.showWidget(0);
		}
	}

	public void showErrorMessage(boolean pShow) {
		if (pShow) {
			statusDeckPanel.showWidget(3);
		} else {
			statusDeckPanel.showWidget(0);
		}
	}

	@Override
	public String getText() {
		return textArea.getValue();
	}

	@Override
	public void setText(String pText) {
		textArea.setValue(pText);
	}
}
